package com.guoranhao.web.util;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class OSUtil {
    private static boolean isCheckedOsFlag = false;
    private static boolean isWinOs = true;

    public static boolean isWindowsOS() {
        if (isCheckedOsFlag) {
            return isWinOs;
        }
        String osName = System.getProperty("os.name");
        isWinOs = (osName != null) && ((osName.startsWith("Win")) || (osName.startsWith("win")));
        isCheckedOsFlag = true;

        return isWinOs;
    }

    public static String getLocalIP() {
        try {
            InetAddress address = InetAddress.getLocalHost();
            return address.getHostAddress();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String[] getAllLocalHostIP() {
        List<String> res = new ArrayList();
        try {
            Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
            InetAddress ip = null;
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = (NetworkInterface) netInterfaces.nextElement();

                Enumeration<InetAddress> nii = ni.getInetAddresses();
                while (nii.hasMoreElements()) {
                    ip = (InetAddress) nii.nextElement();
                    if (ip.getHostAddress().indexOf(":") == -1) {
                        res.add(ip.getHostAddress());
                    }
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return (String[]) res.toArray(new String[0]);
    }

    public static String getLocalMac() {
        return getLocalMac("-");
    }

    public static String getLocalMac(String split) {
        try {
            InetAddress address = InetAddress.getLocalHost();
            NetworkInterface ni = NetworkInterface.getByInetAddress(address);


            byte[] mac = ni.getHardwareAddress();
            String sMAC = "";
            if (split == null) {
                split = "-";
            }
            Formatter formatter = new Formatter();
            for (int i = 0; i < mac.length; i++) {
                sMAC = formatter.format(Locale.getDefault(), "%02X%s", new Object[]{Byte.valueOf(mac[i]), i < mac.length - 1 ? split : ""}).toString();
            }
            return sMAC;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String[] getAllMacAddressByCmd() {
        List<String> macList = new ArrayList();

        String os = System.getProperty("os.name");
        if ((os != null) && (os.startsWith("Windows"))) {
            try {
                String command = "cmd.exe /c ipconfig /all";
                Process p = Runtime.getRuntime().exec(command);


                BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));

                String line = null;
                while ((line = br.readLine()) != null) {
                    line = line.trim();
                    if (line.length() != 0) {
                        if (line.indexOf("Physical Address") > -1) {
                            int index = line.indexOf(":") + 2;

                            String mac = line.substring(index);
                            if ((mac != null) && (!"".equals(mac.trim()))) {
                                macList.add(mac.trim());
                            }
                        } else if (line.indexOf("物理地址") > -1) {
                            int index = line.indexOf(":") + 2;
                            String mac = line.substring(index);
                            if ((mac != null) && (!"".equals(mac.trim()))) {
                                macList.add(mac.trim());
                            }
                        }
                    }
                }
                br.close();
            } catch (IOException e) {
            }
        }
        int len = macList.size();
        return (String[]) macList.toArray(new String[len]);
    }

    public static String getLocalMacFromLinux() {
        String mac = null;
        try {
            Process p = new ProcessBuilder(new String[]{"ifconfig"}).start();
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                Pattern pat = Pattern.compile("\\b\\w+:\\w+:\\w+:\\w+:\\w+:\\w+\\b");
                Matcher mat = pat.matcher(line);
                if (mat.find()) {
                    mac = mat.group(0);
                }
            }
            br.close();
            if (mac != null) {
                while (mac.indexOf(":") > -1) {
                    mac = mac.replace(":", "-");
                }
                mac = mac.toUpperCase();
            }
        } catch (IOException e) {
        }
        return mac;
    }

    public static String[] getAllLocalMacsFromLinux() {
        List<String> macs = new ArrayList();
        try {
            Process p = new ProcessBuilder(new String[]{"ifconfig"}).start();
            BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = br.readLine()) != null) {
                Pattern pat = Pattern.compile("\\b\\w+:\\w+:\\w+:\\w+:\\w+:\\w+\\b");
                Matcher mat = pat.matcher(line);
                if (mat.find()) {
                    String mac = mat.group(0);
                    if (mac != null) {
                        while (mac.indexOf(":") > -1) {
                            mac = mac.replace(":", "-");
                        }
                        mac = mac.toUpperCase();


                        macs.add(mac);
                    }
                }
            }
            br.close();
        } catch (IOException e) {
        }
        int len = macs.size();
        return (String[]) macs.toArray(new String[len]);
    }

    public static void main(String[] args) {
        String line = "ether 00:0c:29:3c:45:dd  txqueuelen 1000  (Ethernet)";

        Pattern pat = Pattern.compile("\\b\\w+:\\w+:\\w+:\\w+:\\w+:\\w+\\b");
        Matcher mat = pat.matcher(line);
        if (mat.find()) {
            String mac = mat.group(0);
            System.out.println("======================" + mac);
        }
    }
}

